﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using FredCK.FCKeditorV2;
using log4net;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.work_plan.show
{
    public partial class work_plan :  ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
     
        public string GetStatusDesc(int status)
        {
            switch (status)
            {
                case 0:
                    return "<font color=\"red\"><b>未开始</font>";

                case 1:
                    return "<font color=\"blue\"><b>进行中</font>";

                case 2:
                    return "<font color=\"green\"><b>完成</font>";
            }
            return "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string str = (Request.QueryString["date"] != null) ? Request.QueryString["date"].ToString() : "1";
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[Plan_Work].[PlanId],");
            builder.Append("\t[Plan_Work].[Name],");
            builder.Append("\t[Plan_Work].[BeginDate],");
            builder.Append("\t[Plan_Work].[EndDate],");
            builder.Append("\t[Plan_Type].[TypeName],");
            builder.Append("\tdbo.GetDeptNames([Plan_Work].[ToId]) AS ToName,");
            builder.Append("\t[Plan_Work].[CompletedPercent],");
            builder.Append("\t[Plan_Work].[Status],");
            builder.Append("\t[Users].[UserName] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Plan_Work], [dbo].[Plan_Type], [dbo].[Users] ");
            builder.Append("WHERE ");
            builder.Append("\t[Plan_Work].[TypeId] = [Plan_Type].[TypeId] ");
            builder.Append("\tAND [Plan_Work].[Creator] = [Users].[UserId] ");
            string str2 = str;
            if (str2 != null)
            {
                if (!(str2 == "1"))
                {
                    if (str2 == "2")
                    {
                        string str3 = DateTime.Now.AddDays((double)(1 - ((DateTime.Now.DayOfWeek == DayOfWeek.Sunday) ? (DayOfWeek.Saturday | DayOfWeek.Monday) : DateTime.Now.DayOfWeek))).ToString("yyyy-MM-dd");
                        string str4 = DateTime.Now.AddDays((double)(7 - ((DateTime.Now.DayOfWeek == DayOfWeek.Sunday) ? (DayOfWeek.Saturday | DayOfWeek.Monday) : DateTime.Now.DayOfWeek))).ToString("yyyy-MM-dd");
                        builder.Append(" AND NOT ((ISNULL([Plan_Work].[BeginDate],'1900-1-1') > '" + str4 + "') OR (ISNULL([Plan_Work].[EndDate],'2100-1-1') < '" + str3 + "')) ");
                    }
                    else if (str2 == "3")
                    {
                        string str5 = string.Concat(new object[] { DateTime.Now.Year, "-", DateTime.Now.Month, "-1" });
                        string str6 = string.Concat(new object[] { DateTime.Now.Year, "-", DateTime.Now.Month, "-", DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month).ToString() });
                        builder.Append(" AND NOT ((ISNULL([Plan_Work].[BeginDate],'1900-1-1') > '" + str6 + "') OR (ISNULL([Plan_Work].[EndDate],'2100-1-1') < '" + str5 + "')) ");
                    }
                }
                else
                {
                    builder.Append(" AND ((ISNULL([Plan_Work].[BeginDate],'1900-1-1') <= '" + DateTime.Now.ToString("yyyy-MM-dd") + "') AND (ISNULL([Plan_Work].[EndDate],'2100-1-1') >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "')) ");
                }
            }
            try
            {
                string commandText = builder.ToString() + " AND (CAST([Plan_Work].[ToId] AS nvarchar(2000)) = 'All_DEPT') ORDER BY [Plan_Work].[PlanId] DESC";
                DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText).Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.rptAllDept.DataSource = table;
                    this.rptAllDept.DataBind();
                }
                else
                {
                    this.litAllDept.Text = base.ShowMessage("无");
                }
                UserCache cache = new UserCache();
                string str8 = builder.ToString() + " AND (CHARINDEX('," + cache.DeptId.ToString() + ",',',' + CAST([Plan_Work].[ToId] AS nvarchar(2000)) + ',') > 0) ORDER BY [Plan_Work].[PlanId] DESC";
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , str8).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.rptDept.DataSource = table2;
                    this.rptDept.DataBind();
                }
                else
                {
                    this.litDept.Text = base.ShowMessage("无");
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
        }

 

    }
}
